package org.zbclan.bf2.rcon;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

public class HardwareQuery extends ShellCommand {

	private File file;

	public String getMotherboardSN() throws IOException {

		String result = "";

		if (file == null) {
			synchronized (this) {
				if (file == null) {
					file = File.createTempFile("tmp", ".vbs");
					file.deleteOnExit();
				}
			}
		}

		synchronized (file) {
			// FileWriter fw = new java.io.FileWriter(file);
			//
			// String vbs =
			// "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
			// + "Set colItems = objWMIService.ExecQuery _ \n"
			// + "   (\"Select * from Win32_BaseBoard\") \n"
			// + "For Each objItem in colItems \n"
			// + "    Wscript.Echo objItem.SerialNumber \n"
			// + "    exit for  ' do the first cpu only! \n" + "Next \n";
			//
			// fw.write(vbs);
			// fw.close();

			OutputStream output = new BufferedOutputStream(
					new FileOutputStream(file));
			try {
				// InputStream input =
				// Thread.currentThread().getContextClassLoader()
				// .getResourceAsStream("realhowto.vbs");
				InputStream input = HardwareQuery.class
						.getResourceAsStream("realhowto.vbs");
				try {
					IOUtils.copy(input, output);
				} finally {
					input.close();
				}
			} finally {
				output.close();
			}

			result = execute("cscript", "/NoLogo", file.getPath());
		}

		return result.trim();
	}
}